package com.liuxinlong.jobs;

import com.liuxinlong.modules.dao.UserDao;
import com.liuxinlong.modules.entity.User;
import com.liuxinlong.modules.system.dto.WechatUserDto;
import com.liuxinlong.modules.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 使用quartz框架，同步企业微信用户信息（异步处理）
 */
@Service
@Slf4j
@Component("syncWechatUserTask")
public class SyncWechatUserTask {

    @Autowired
    private UserService userService;

    @Autowired
    private UserDao userDao;

    /**
     * 业务逻辑
     */
    public void reptilian(){
        log.info("SyncWechatUserTask start...");
        List<WechatUserDto> wechatUserDtoList = userService.queryWechatUserList();
        Map<String,String> wechatUserMap = new HashMap<>();
        for(WechatUserDto wud: wechatUserDtoList) {
            wechatUserMap.put(wud.getUserSn(),wud.getUserId());
        }
        List<User> userList = userDao.getUserList();
        List<User> updateList = new ArrayList<>();
        for (User user:userList) {
            if (!wechatUserMap.containsKey(user.getSn())) {
               continue;
            }
            String empId = wechatUserMap.get(user.getSn());
//            if (StringUtils.equals(empId,user.getEmpId())) {
//                continue;
//            }
//            user.setEmpId(empId);
            updateList.add(user);

        }
        log.info("SyncWechatUserTask end...");
    }
}

